{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  数据重采样 ###\n",
    "- 时间数据由一个频率转换到另一个频率\n",
    "- 降采样\n",
    "- 升采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -1.025562\n",
       "2011-01-02    0.410895\n",
       "2011-01-03    0.660311\n",
       "2011-01-04    0.710293\n",
       "2011-01-05    0.444985\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2011', periods=90, freq='D')\n",
    "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-31    2.510102\n",
       "2011-02-28    0.583209\n",
       "2011-03-31    2.749411\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('M').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01    0.045643\n",
       "2011-01-04   -2.255206\n",
       "2011-01-07    0.571142\n",
       "2011-01-10    0.835032\n",
       "2011-01-13   -0.396766\n",
       "2011-01-16   -1.156253\n",
       "2011-01-19   -1.286884\n",
       "2011-01-22    2.883952\n",
       "2011-01-25    1.566908\n",
       "2011-01-28    1.435563\n",
       "2011-01-31    0.311565\n",
       "2011-02-03   -2.541235\n",
       "2011-02-06    0.317075\n",
       "2011-02-09    1.598877\n",
       "2011-02-12   -1.950509\n",
       "2011-02-15    2.928312\n",
       "2011-02-18   -0.733715\n",
       "2011-02-21    1.674817\n",
       "2011-02-24   -2.078872\n",
       "2011-02-27    2.172320\n",
       "2011-03-02   -2.022104\n",
       "2011-03-05   -0.070356\n",
       "2011-03-08    1.276671\n",
       "2011-03-11   -2.835132\n",
       "2011-03-14   -1.384113\n",
       "2011-03-17    1.517565\n",
       "2011-03-20   -0.550406\n",
       "2011-03-23    0.773430\n",
       "2011-03-26    2.244319\n",
       "2011-03-29    2.951082\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('3D').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01    0.015214\n",
       "2011-01-04   -0.751735\n",
       "2011-01-07    0.190381\n",
       "2011-01-10    0.278344\n",
       "2011-01-13   -0.132255\n",
       "2011-01-16   -0.385418\n",
       "2011-01-19   -0.428961\n",
       "2011-01-22    0.961317\n",
       "2011-01-25    0.522303\n",
       "2011-01-28    0.478521\n",
       "2011-01-31    0.103855\n",
       "2011-02-03   -0.847078\n",
       "2011-02-06    0.105692\n",
       "2011-02-09    0.532959\n",
       "2011-02-12   -0.650170\n",
       "2011-02-15    0.976104\n",
       "2011-02-18   -0.244572\n",
       "2011-02-21    0.558272\n",
       "2011-02-24   -0.692957\n",
       "2011-02-27    0.724107\n",
       "2011-03-02   -0.674035\n",
       "2011-03-05   -0.023452\n",
       "2011-03-08    0.425557\n",
       "2011-03-11   -0.945044\n",
       "2011-03-14   -0.461371\n",
       "2011-03-17    0.505855\n",
       "2011-03-20   -0.183469\n",
       "2011-03-23    0.257810\n",
       "2011-03-26    0.748106\n",
       "2011-03-29    0.983694\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day3Ts = ts.resample('3D').mean()\n",
    "day3Ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2011-01-01    0.015214\n",
      "2011-01-02         NaN\n",
      "2011-01-03         NaN\n",
      "2011-01-04   -0.751735\n",
      "2011-01-05         NaN\n",
      "2011-01-06         NaN\n",
      "2011-01-07    0.190381\n",
      "2011-01-08         NaN\n",
      "2011-01-09         NaN\n",
      "2011-01-10    0.278344\n",
      "2011-01-11         NaN\n",
      "2011-01-12         NaN\n",
      "2011-01-13   -0.132255\n",
      "2011-01-14         NaN\n",
      "2011-01-15         NaN\n",
      "2011-01-16   -0.385418\n",
      "2011-01-17         NaN\n",
      "2011-01-18         NaN\n",
      "2011-01-19   -0.428961\n",
      "2011-01-20         NaN\n",
      "2011-01-21         NaN\n",
      "2011-01-22    0.961317\n",
      "2011-01-23         NaN\n",
      "2011-01-24         NaN\n",
      "2011-01-25    0.522303\n",
      "2011-01-26         NaN\n",
      "2011-01-27         NaN\n",
      "2011-01-28    0.478521\n",
      "2011-01-29         NaN\n",
      "2011-01-30         NaN\n",
      "                ...   \n",
      "2011-02-28         NaN\n",
      "2011-03-01         NaN\n",
      "2011-03-02   -0.674035\n",
      "2011-03-03         NaN\n",
      "2011-03-04         NaN\n",
      "2011-03-05   -0.023452\n",
      "2011-03-06         NaN\n",
      "2011-03-07         NaN\n",
      "2011-03-08    0.425557\n",
      "2011-03-09         NaN\n",
      "2011-03-10         NaN\n",
      "2011-03-11   -0.945044\n",
      "2011-03-12         NaN\n",
      "2011-03-13         NaN\n",
      "2011-03-14   -0.461371\n",
      "2011-03-15         NaN\n",
      "2011-03-16         NaN\n",
      "2011-03-17    0.505855\n",
      "2011-03-18         NaN\n",
      "2011-03-19         NaN\n",
      "2011-03-20   -0.183469\n",
      "2011-03-21         NaN\n",
      "2011-03-22         NaN\n",
      "2011-03-23    0.257810\n",
      "2011-03-24         NaN\n",
      "2011-03-25         NaN\n",
      "2011-03-26    0.748106\n",
      "2011-03-27         NaN\n",
      "2011-03-28         NaN\n",
      "2011-03-29    0.983694\n",
      "Freq: D, Length: 88, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(day3Ts.resample('D').asfreq())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  插值方法： ###\n",
    "- ffill 空值取前面的值\n",
    "- bfill 空值取后面的值\n",
    "- interpolate 线性取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01    0.015214\n",
       "2011-01-02    0.015214\n",
       "2011-01-03         NaN\n",
       "2011-01-04   -0.751735\n",
       "2011-01-05   -0.751735\n",
       "2011-01-06         NaN\n",
       "2011-01-07    0.190381\n",
       "2011-01-08    0.190381\n",
       "2011-01-09         NaN\n",
       "2011-01-10    0.278344\n",
       "2011-01-11    0.278344\n",
       "2011-01-12         NaN\n",
       "2011-01-13   -0.132255\n",
       "2011-01-14   -0.132255\n",
       "2011-01-15         NaN\n",
       "2011-01-16   -0.385418\n",
       "2011-01-17   -0.385418\n",
       "2011-01-18         NaN\n",
       "2011-01-19   -0.428961\n",
       "2011-01-20   -0.428961\n",
       "2011-01-21         NaN\n",
       "2011-01-22    0.961317\n",
       "2011-01-23    0.961317\n",
       "2011-01-24         NaN\n",
       "2011-01-25    0.522303\n",
       "2011-01-26    0.522303\n",
       "2011-01-27         NaN\n",
       "2011-01-28    0.478521\n",
       "2011-01-29    0.478521\n",
       "2011-01-30         NaN\n",
       "                ...   \n",
       "2011-02-28    0.724107\n",
       "2011-03-01         NaN\n",
       "2011-03-02   -0.674035\n",
       "2011-03-03   -0.674035\n",
       "2011-03-04         NaN\n",
       "2011-03-05   -0.023452\n",
       "2011-03-06   -0.023452\n",
       "2011-03-07         NaN\n",
       "2011-03-08    0.425557\n",
       "2011-03-09    0.425557\n",
       "2011-03-10         NaN\n",
       "2011-03-11   -0.945044\n",
       "2011-03-12   -0.945044\n",
       "2011-03-13         NaN\n",
       "2011-03-14   -0.461371\n",
       "2011-03-15   -0.461371\n",
       "2011-03-16         NaN\n",
       "2011-03-17    0.505855\n",
       "2011-03-18    0.505855\n",
       "2011-03-19         NaN\n",
       "2011-03-20   -0.183469\n",
       "2011-03-21   -0.183469\n",
       "2011-03-22         NaN\n",
       "2011-03-23    0.257810\n",
       "2011-03-24    0.257810\n",
       "2011-03-25         NaN\n",
       "2011-03-26    0.748106\n",
       "2011-03-27    0.748106\n",
       "2011-03-28         NaN\n",
       "2011-03-29    0.983694\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day3Ts.resample('D').ffill(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01    0.015214\n",
       "2011-01-02         NaN\n",
       "2011-01-03   -0.751735\n",
       "2011-01-04   -0.751735\n",
       "2011-01-05         NaN\n",
       "2011-01-06    0.190381\n",
       "2011-01-07    0.190381\n",
       "2011-01-08         NaN\n",
       "2011-01-09    0.278344\n",
       "2011-01-10    0.278344\n",
       "2011-01-11         NaN\n",
       "2011-01-12   -0.132255\n",
       "2011-01-13   -0.132255\n",
       "2011-01-14         NaN\n",
       "2011-01-15   -0.385418\n",
       "2011-01-16   -0.385418\n",
       "2011-01-17         NaN\n",
       "2011-01-18   -0.428961\n",
       "2011-01-19   -0.428961\n",
       "2011-01-20         NaN\n",
       "2011-01-21    0.961317\n",
       "2011-01-22    0.961317\n",
       "2011-01-23         NaN\n",
       "2011-01-24    0.522303\n",
       "2011-01-25    0.522303\n",
       "2011-01-26         NaN\n",
       "2011-01-27    0.478521\n",
       "2011-01-28    0.478521\n",
       "2011-01-29         NaN\n",
       "2011-01-30    0.103855\n",
       "                ...   \n",
       "2011-02-28         NaN\n",
       "2011-03-01   -0.674035\n",
       "2011-03-02   -0.674035\n",
       "2011-03-03         NaN\n",
       "2011-03-04   -0.023452\n",
       "2011-03-05   -0.023452\n",
       "2011-03-06         NaN\n",
       "2011-03-07    0.425557\n",
       "2011-03-08    0.425557\n",
       "2011-03-09         NaN\n",
       "2011-03-10   -0.945044\n",
       "2011-03-11   -0.945044\n",
       "2011-03-12         NaN\n",
       "2011-03-13   -0.461371\n",
       "2011-03-14   -0.461371\n",
       "2011-03-15         NaN\n",
       "2011-03-16    0.505855\n",
       "2011-03-17    0.505855\n",
       "2011-03-18         NaN\n",
       "2011-03-19   -0.183469\n",
       "2011-03-20   -0.183469\n",
       "2011-03-21         NaN\n",
       "2011-03-22    0.257810\n",
       "2011-03-23    0.257810\n",
       "2011-03-24         NaN\n",
       "2011-03-25    0.748106\n",
       "2011-03-26    0.748106\n",
       "2011-03-27         NaN\n",
       "2011-03-28    0.983694\n",
       "2011-03-29    0.983694\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day3Ts.resample('D').bfill(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01    0.015214\n",
       "2011-01-02   -0.240435\n",
       "2011-01-03   -0.496085\n",
       "2011-01-04   -0.751735\n",
       "2011-01-05   -0.437697\n",
       "2011-01-06   -0.123658\n",
       "2011-01-07    0.190381\n",
       "2011-01-08    0.219702\n",
       "2011-01-09    0.249023\n",
       "2011-01-10    0.278344\n",
       "2011-01-11    0.141478\n",
       "2011-01-12    0.004611\n",
       "2011-01-13   -0.132255\n",
       "2011-01-14   -0.216643\n",
       "2011-01-15   -0.301030\n",
       "2011-01-16   -0.385418\n",
       "2011-01-17   -0.399932\n",
       "2011-01-18   -0.414447\n",
       "2011-01-19   -0.428961\n",
       "2011-01-20    0.034465\n",
       "2011-01-21    0.497891\n",
       "2011-01-22    0.961317\n",
       "2011-01-23    0.814979\n",
       "2011-01-24    0.668641\n",
       "2011-01-25    0.522303\n",
       "2011-01-26    0.507709\n",
       "2011-01-27    0.493115\n",
       "2011-01-28    0.478521\n",
       "2011-01-29    0.353632\n",
       "2011-01-30    0.228744\n",
       "                ...   \n",
       "2011-02-28    0.258060\n",
       "2011-03-01   -0.207988\n",
       "2011-03-02   -0.674035\n",
       "2011-03-03   -0.457174\n",
       "2011-03-04   -0.240313\n",
       "2011-03-05   -0.023452\n",
       "2011-03-06    0.126218\n",
       "2011-03-07    0.275887\n",
       "2011-03-08    0.425557\n",
       "2011-03-09   -0.031310\n",
       "2011-03-10   -0.488177\n",
       "2011-03-11   -0.945044\n",
       "2011-03-12   -0.783820\n",
       "2011-03-13   -0.622595\n",
       "2011-03-14   -0.461371\n",
       "2011-03-15   -0.138962\n",
       "2011-03-16    0.183446\n",
       "2011-03-17    0.505855\n",
       "2011-03-18    0.276080\n",
       "2011-03-19    0.046306\n",
       "2011-03-20   -0.183469\n",
       "2011-03-21   -0.036376\n",
       "2011-03-22    0.110717\n",
       "2011-03-23    0.257810\n",
       "2011-03-24    0.421242\n",
       "2011-03-25    0.584674\n",
       "2011-03-26    0.748106\n",
       "2011-03-27    0.826636\n",
       "2011-03-28    0.905165\n",
       "2011-03-29    0.983694\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day3Ts.resample('D').interpolate('linear')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
